#include "imgui/Editor.h"

bool ImGui::Editor::InputMultiString(const std::string& label, std::string& value, int row)
{
    ImGui::PushID(&value);
    ImVector<char> my_str;
    for (auto ch : value)
    {
        my_str.push_back(ch);
    }

    my_str.push_back(0);

    bool res = InputMultiString(label, my_str, row);

    value.clear();
    for (auto r : my_str)
    {
        value.push_back(r);
    }
    value = value.substr(0, value.length() - 1);

    ImGui::PopID();
    return res;
}

bool ImGui::Editor::InputMultiString(const std::string label, ImVector<char>& my_str, int row)
{

    if (my_str.empty())
    {
        my_str.push_back(0);
    }
    bool res = ImGui::InputTextMultiline(label.c_str(), my_str.begin(), (size_t)my_str.size(), ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * row),
        ImGuiInputTextFlags_CallbackResize, Funcs::MyResizeCallback, (void*)&my_str);

    return res;
}

bool ImGui::Editor::InputString(const std::string label, std::string& value)
{
    ImVector<char> my_str;

    for (char ch : value)
    {
        my_str.push_back(ch);
    }

    my_str.push_back(0);

    bool res = ImGui::InputText(label.c_str(), my_str.begin(), (size_t)my_str.size(),
        ImGuiInputTextFlags_CallbackResize, Funcs::MyResizeCallback, (void*)&my_str);

    value.clear();
    for (const char& rch : my_str)
    {
        value.push_back(rch);
    }
    //value = value.substr(0, value.length() - 1);
    value = value.c_str();
    return res;
};

template<>
bool ImGui::Editor::Edit<std::string>(const std::string& label, std::string& inst)
{
    bool res = InputString(label, inst);
    return res;
}


template<>
bool ImGui::Editor::Edit<int>(const std::string& label, int& inst)
{
    bool res = ImGui::InputInt(label.c_str(), &inst);
    return res;
}

template<>
bool ImGui::Editor::Edit<unsigned int>(const std::string& label, unsigned& inst)
{
    bool res = ImGui::InputInt(label.c_str(), (int*)&inst);
    return res;
}

template<>
bool ImGui::Editor::Edit<bool>(const std::string& label, bool& inst)
{
    bool res = ImGui::Checkbox(label.c_str(), &inst);
    return res;
}



bool ImGui::Editor::Checkbox(const std::string& label, int& open, int index)
{
    bool bitMap = GetBit(open, index);
    bool res = ImGui::Checkbox(label.c_str(), &bitMap);
    SetBit(open, index, bitMap);

    return res;

}

bool ImGui::Editor::TreeNode(const std::string& label, int& open, int index, ImGuiTreeNodeFlags flag)
{
    bool bitMap = GetBit(open, index);
    if(bitMap)
        ImGui::SetNextItemOpen(bitMap);
    bool res = ImGui::TreeNodeEx(label.c_str(), flag);

    SetBit(open, index, bitMap);

    return res;
}
